package com.cskaoyan.controller.user;

import com.cskaoyan.bean.BaseParam;
import com.cskaoyan.bean.BaseRespVo;
import com.cskaoyan.bean.PageDataVo;
import com.cskaoyan.bean.user.User;
import com.cskaoyan.bean.user.UserIdParam;
import com.cskaoyan.bean.user.UserUpdateVO;
import com.cskaoyan.exception.ParamValidationException;
import com.cskaoyan.service.user.UserService;
import com.cskaoyan.utils.ValidationUtil;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.BindingResult;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.validation.Valid;

@RestController
@RequestMapping("admin")
public class UserController {
    @Autowired
    UserService userService;

//    @RequiresPermissions("GET /admin/user/list")
    @RequestMapping("user/list")
    public BaseRespVo getUser(String username, String mobile, BaseParam baseParam) {
        PageDataVo pageDataVo = userService.selectUser(username, mobile, baseParam);
        return BaseRespVo.ok(pageDataVo);
    }

//    @RequiresPermissions("GET /admin/user/detail")
    @RequestMapping("user/detail")
    public BaseRespVo getUserById(String id) throws ParamValidationException {
        Integer paramId;
        try {
            paramId = Integer.valueOf(id);
        } catch (Exception e) {
            throw new ParamValidationException("请注意参数类型！");
        }
        User user = userService.selectUserById(paramId);
        return BaseRespVo.ok(user);
    }

//    @RequiresPermissions("POST /admin/user/update")
    @RequestMapping("user/update")
    public BaseRespVo updateUser(@RequestBody User user){
        userService.updateUser(user);
        return BaseRespVo.ok();
    }

//    @RequiresPermissions("GET /admin/address/list")
    @RequestMapping("address/list")
    public BaseRespVo listAddress(String name, @Validated UserIdParam userId, BindingResult bindingResult, BaseParam param) {
        ValidationUtil.valid(bindingResult);
        PageDataVo pageDataVo = userService.listAddress(name, userId.getUserId(), param);
        return BaseRespVo.ok(pageDataVo);
    }

//    @RequiresPermissions("GET /admin/collect/list")
    @RequestMapping("collect/list")
    public BaseRespVo listCollect(@Valid UserIdParam userId, BindingResult bindingResult,Integer valueId, BaseParam param ){
        ValidationUtil.valid(bindingResult);
        PageDataVo pageDataVo = userService.listCollect(userId.getUserId(), valueId, param);
        return BaseRespVo.ok(pageDataVo);

    }

//    @RequiresPermissions("GET /admin/footprint/list")
    @RequestMapping("footprint/list")
    public BaseRespVo listFootprint(Integer userId, Integer goodsId,BaseParam baseParam){
        PageDataVo pageDataVo = userService.listFootprint(userId,goodsId,baseParam);
        return BaseRespVo.ok(pageDataVo);
    }

//    @RequiresPermissions("GET /admin/history/list")
    @RequestMapping("history/list")
    public BaseRespVo listHistory(Integer userId, String keyword, BaseParam baseParam){
       PageDataVo pageDataVo =  userService.listHistory(userId,keyword,baseParam);
       return BaseRespVo.ok(pageDataVo);
    }

//    @RequiresPermissions("GET /admin/feedback/list")
    @RequestMapping("feedback/list")
    public BaseRespVo listFeedback(String userName, Integer feedbackId,BaseParam baseParam){
       PageDataVo pageDataVo =  userService.listFeedback(userName,feedbackId,baseParam);
       return  BaseRespVo.ok(pageDataVo);
    }
}
